﻿
var mysql = require('mysql');
var pool = mysql.createPool({
    host: '192.168.1.253',
    port: 3306,
    database: 'platform',
    user: 'root',
    password: 'root'
});
function storage_tomysql(data) {
    pool.getConnection(function (err, connection) {
        if (err) {
            console.log('mysql connect failure');
            return -2;
        }
        else {
            var sql = 'INSERT INTO modbus_sensor_table(press,temp,time,deviceid) VALUES(?,?,?,?)';
            var param = [data.press, data.temp, data.time, data.deviceid];

            connection.query(sql, param, function (err, result) {
                if (err) {
                    //connection.query("select * from sensor")
                    console.log("error insert");
                    connection.release();
                    return -3;
                }
                else {
                    console.log("insert to db:" + param);
                    connection.release();
                    return 0;
                }
            })
        }
    });
}
